package com.kaochong.xcx.web.controller;

import com.kaochong.xcx.enums.ResultEnum;
import com.kaochong.xcx.service.PrizesService;
import com.kaochong.xcx.utils.response.ResponseUtils;
import com.kaochong.xcx.utils.response.ResultVO;
import com.kaochong.xcx.web.aspect.CurrentUser;
import com.kaochong.xcx.web.controller.base.BasicController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/recite")
@Slf4j
public class PrizeController extends BasicController {

    @Autowired
    private PrizesService prizesService;
    /**
     * 查询当前活动
     *
     * @return
     */
    @GetMapping("/prize")
    public ResultVO currentPrize(Map<String, Object> map) {
        map.put("prizeInfo",prizesService.getCurrentPrize(super.takeUser().getId()));
        return ResponseUtils.success(map);
    }

    /**
     * 查询我的抽奖历史列表 分页
     * 备注：8月6号修改需求为，从“我的抽奖记录“概念调整为“抽奖记录”
     *
     * @param pages
     * @param rows
     * @param map
     * @return
     */
    @GetMapping("/prize/history")
    public ResultVO getPrizeHistory(@RequestParam(name = "pages", required = false, defaultValue = "1") Integer pages,
                                    @RequestParam(name = "rows", required = false, defaultValue = "20") Integer rows, Map<String, Object> map) {
        CurrentUser user = super.takeUser();
        Integer offset = (pages - 1) * rows;
        map.put("prizeList", prizesService.findPrizeUserVO(user.getId(), offset, rows));
        return ResponseUtils.success(map);
    }

    /**
     * 通过日期查询当天获奖幸运儿
     *
     * @param PrizeDate
     * @param map
     * @return
     */
    @GetMapping("/prize/lucky")
    public ResultVO getLuckyUser(@RequestParam(name = "prizedate", required = false) String PrizeDate, Map<String, Object> map) {
        if (PrizeDate != null && !("").equals(PrizeDate)) {
            return ResponseUtils.success(prizesService.getLuckyUser(PrizeDate));
        } else {
            return ResponseUtils.error(ResultEnum.PARAM_ERROR);
        }
    }

    /**
     * 获奖后存储用户手机号
     *
     * @param phone
     * @return
     */
    @PostMapping("/prize")
    public ResultVO savePhone(@RequestParam(name = "phone") String phone) {
        prizesService.savePhone(super.takeUser().getId(), phone);
        return ResponseUtils.success();
    }

    /**
     * 好友助力
     * @param openId
     * @param prizeId
     * @param helpDate
     * @return
     */
    @PostMapping("/prize/help")
    public ResultVO friendHelp(@RequestParam(name = "openid") String openId,
                               @RequestParam(name = "prizeid") Integer prizeId,
                               @RequestParam(name = "helpdate") String helpDate) {
        prizesService.friendHelp(super.takeUser(), openId, prizeId, helpDate);
        return ResponseUtils.success();
    }

    @GetMapping("/prize/findLuckyMan")
    public ResultVO findLuckyMan(@RequestParam(name = "prizedate") String prizeDate, Map<String, Object> map){
        map.put("luckyList",prizesService.getAllLuckyUser(prizeDate));
        return ResponseUtils.success(map);
    }
}
